package com.sikao.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;

import com.sikao.app.R;
import com.sikao.app.bean.AddStepPricesSubmitBean;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.ui.AddStepPricesActivity;
import com.sikao.app.utils.Utils;

public class MyAddsStepPricesListItemAdapter extends BaseAdapter {

	private List<AddStepPricesSubmitBean.StepPricesBean> objects = new ArrayList<AddStepPricesSubmitBean.StepPricesBean>();

	private Context context;
	private LayoutInflater layoutInflater;
	private String goodsUnit;
	Callback callback;

	public MyAddsStepPricesListItemAdapter(Context context, List<AddStepPricesSubmitBean.StepPricesBean> objects, Callback callback, String goodsUnit) {
		this.context = context;
		this.objects = objects;
		this.callback = callback;
		this.layoutInflater = LayoutInflater.from(context);
		this.goodsUnit = goodsUnit;
	}

	@Override
	public int getCount() {
		return objects.size();
	}

	@Override
	public AddStepPricesSubmitBean.StepPricesBean getItem(int position) {
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private Integer index = -1;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//if (convertView == null) {防止数据复用
		convertView = layoutInflater.inflate(R.layout.my_adds_step_prices_list_item, null);
		convertView.setTag(new ViewHolder(convertView));
		//}
		initializeViews(getItem(position), (ViewHolder) convertView.getTag(), position);

		return convertView;
	}

	private void initializeViews(AddStepPricesSubmitBean.StepPricesBean object, ViewHolder holder, final int position) {
		//TODO implement
		holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				callback.notice(position);
			}
		});

		if ("0".equals(AddStepPricesActivity.stepPricesBeanList.get(position).getNumber())) {
			holder.etNumber.setHint("请输入数量");
		} else {
			holder.etNumber.setText(AddStepPricesActivity.stepPricesBeanList.get(position).getNumber());
		}
		if ("0".equals(AddStepPricesActivity.stepPricesBeanList.get(position).getPrice())) {
			holder.etPrice.setHint("请输入价格");
		} else {
			holder.etPrice.setText(AddStepPricesActivity.stepPricesBeanList.get(position).getPrice());
		}
		//设置单位显示的值
		holder.tvUnit.setText("(" + goodsUnit + ")：");
		holder.tvUnitPrice.setText("(元/" + goodsUnit + ")：");

		holder.etNumber.setOnTouchListener(new View.OnTouchListener() {

			EditText et = null;

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				et = (EditText) v;

				et.addTextChangedListener(new TextWatcher() {
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count, int after) {

					}

					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {

					}

					@Override
					public void afterTextChanged(Editable s) {
						String str = et.getText().toString();

						if (!TextUtils.isEmpty(str) && Utils.isNum(str)) {
							AddStepPricesActivity.stepPricesBeanList.get(position).setNumber(str);
						}
					}
				});
				return false;
			}
		});
		// holder.etPrice.setFilters(new InputFilter[]{Utils.lengthfilter});
		holder.etPrice.setOnTouchListener(new View.OnTouchListener() {

			EditText et = null;

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				et = (EditText) v;

				et.addTextChangedListener(new TextWatcher() {
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count, int after) {

					}

					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {

					}

					@Override
					public void afterTextChanged(Editable s) {



                       /* String temp = s.toString();
						int posDot = temp.indexOf(".");
                        if (posDot <= 0) return;
                        if (temp.length() - posDot - 1 > 2)
                        {
                            s.delete(posDot + 3, posDot + 4);
                        }*/

						AddStepPricesActivity.stepPricesBeanList.get(position).setPrice(s.toString());
				   /* String str = et.getText().toString();

                        if (!TextUtils.isEmpty(str)&& Utils.isNum(str)) {
                            str=Utils.roundForNumber(str,2);
                            Utils.showLog("=====保留两位小数str："+str);
                            if (str.equals(et.getText().toString())){
                                return;
                            }else {
                                et.setText(str);
                            }
                            AddStepPricesActivity.stepPricesBeanList.get(position).setPrice(str);
                        }else{
                             Utils.showToast("请输入正确的价格");
                        }*/
					}
				});
				return false;
			}
		});


	}

	protected class ViewHolder {
		private TextView tvNumber;
		private TextView tvUnit;
		private EditText etNumber;
		private TextView tvPrice;
		private TextView tvUnitPrice;
		private EditText etPrice;
		private Button deleteBtn;

		public ViewHolder(View view) {
			tvNumber = (TextView) view.findViewById(R.id.tv_number);
			tvUnit = (TextView) view.findViewById(R.id.tv_unit);
			etNumber = (EditText) view.findViewById(R.id.et_number);
			tvPrice = (TextView) view.findViewById(R.id.tv_price);
			tvUnitPrice = (TextView) view.findViewById(R.id.tv_unit_price);
			etPrice = (EditText) view.findViewById(R.id.et_price);
			deleteBtn = (Button) view.findViewById(R.id.delete_btn);
		}
	}

}
